//
//  UIView+Alert.m
//  Express
//
//  Created by lhongl on 2025/9/26.
//  Copyright © 2025 QM. All rights reserved.
//

#import "UIView+Alert.h"
#import <objc/runtime.h>
static const void *hl_showTypeKey = &hl_showTypeKey;
static const void *hl_contextDismissKey = &hl_contextDismissKey;

@interface UIView () <UIGestureRecognizerDelegate>

/// 类型
@property (nonatomic, assign) MGShowType hl_showType;

/// 点击背景是否消失
@property (nonatomic, assign) BOOL hl_contextDismiss;

@end
@implementation UIView (Alert)

#pragma mark - 弹框

/// 弹框
/// - Parameters:
///   - type: 类型
///   - superView: 父view
- (void)show:(MGShowType)type inView:(UIView *__nullable)superView {
    if (!superView) {
        superView = [[[UIApplication sharedApplication] delegate] window];
    }
    self.hl_contextDismiss = YES;
    self.hl_showType = type;
    [self showWithType:type inView:superView];
    
}

/// 弹框 【带背景点击消失事件】
/// - Parameters:
///   - type: 类型
///   - superView: 父view
- (void)showWithContextDismiss:(MGShowType)type inView:(UIView * __nullable)superView {
    if (!superView) {
        superView = [[[UIApplication sharedApplication] delegate] window];
    }
    self.hl_contextDismiss = NO;
    self.hl_showType = type;
    [self showWithType:type inView:superView];
}

/// 弹框
/// - Parameters:
///   - type: 类型
///   - superView: 父view
- (void)showWithType:(MGShowType)type inView:(UIView *__nullable)superView {
    switch (type) {
        case ShowAlertType:
            [self alertAnimateWithSuperView:superView showType:ShowAlertType];
            break;
        case ShowAlertFromTopType:
            [self alertAnimateWithSuperView:superView showType:ShowAlertFromTopType];
            break;
        case ShowAlertFromBottomType:
            [self alertAnimateWithSuperView:superView showType:ShowAlertFromBottomType];
            break;
        case ShowBottomType:
            [self bottomAnimateWithSuperView:superView];
            break;
        case ShowTopType:
            [self topAnimateWithSuperView:superView];
            break;
        case ShowLeftType:
            [self leftAnimateWithSuperView:superView];
            break;
        case ShowRightType:
            [self rightAnimateWithSuperView:superView];
           break;
        default:
            break;
    }
}

#pragma mark - 弹框类型

/// alert 弹框
/// - Parameters:
///   - superView: 父View
///   - type: 类型
- (void)alertAnimateWithSuperView:(UIView *)superView showType:(MGShowType)type {
    self.center = superView.center;
    self.alpha = 0.0;
    UIView *backView = [self backgroundViewWithFrame:superView.bounds];
    [backView addSubview:self];
    [superView addSubview:backView];

    switch (type) {
        case ShowAlertType:{
            [UIView animateWithDuration:0.5 animations:^{
                self.alpha = 1.0f;
                backView.backgroundColor = [self backCustomColor];
            }];
        }
            break;
        case ShowAlertFromTopType:{
            CGFloat fromY = -CGRectGetHeight(self.frame);
            self.frame = CGRectMake(CGRectGetMinX(self.frame), fromY, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
            [UIView animateWithDuration:0.5 animations:^{
                self.center = superView.center;
                self.alpha = 1.0f;
                backView.backgroundColor = [self backCustomColor];
            }];
        }
            break;
        case ShowAlertFromBottomType:{
            CGFloat fromY = CGRectGetMaxY(superView.frame);
            self.frame = CGRectMake(CGRectGetMinX(self.frame), fromY, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
            [UIView animateWithDuration:0.5 animations:^{
                self.center = superView.center;
                self.alpha = 1.0f;
                backView.backgroundColor = [self backCustomColor];
            }];
        }
            break;

        default:
            break;
    }
}


/// 左侧弹框
/// - Parameter superView: 父view
- (void)leftAnimateWithSuperView:(UIView *)superView{
    self.center = superView.center;
    CGFloat fromex = CGRectGetMinX(superView.frame) - CGRectGetWidth(self.frame);
    self.frame = CGRectMake(fromex, CGRectGetMinY(self.frame), CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    self.alpha = 0;
    UIView *backView = [self backgroundViewWithFrame:superView.bounds];
    [backView addSubview:self];
    [superView addSubview:backView];
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1.0;
        backView.backgroundColor = [self backCustomColor];
        self.frame = CGRectMake(CGRectGetMinX(superView.frame), CGRectGetMinY(self.frame), CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    }];
}

/// 右侧弹框
/// - Parameter superView: 父view
- (void)rightAnimateWithSuperView:(UIView *)superView{
    self.center = superView.center;
    CGFloat fromex = CGRectGetMaxX(superView.frame);
    CGFloat tox = CGRectGetMaxX(superView.frame) - CGRectGetWidth(self.frame);
    self.frame = CGRectMake(fromex, CGRectGetMinY(self.frame), CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    self.alpha = 0;
    UIView *backView = [self backgroundViewWithFrame:superView.bounds];
    [backView addSubview:self];
    [superView addSubview:backView];
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1.0;
        backView.backgroundColor = [self backCustomColor];
        self.frame = CGRectMake(tox, CGRectGetMinY(self.frame), CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    }];
}

/// 底部弹框
/// - Parameter superView: 父view
- (void)bottomAnimateWithSuperView:(UIView *)superView {
    self.center = superView.center;
    CGFloat fromY = CGRectGetMaxY(superView.frame);
    CGFloat toY = CGRectGetMaxY(superView.frame)-CGRectGetHeight(self.frame);
    self.frame = CGRectMake(CGRectGetMinX(self.frame), fromY, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    self.alpha = 0.0;
    UIView *backView = [self backgroundViewWithFrame:superView.bounds];
    [backView addSubview:self];
    [superView addSubview:backView];
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(CGRectGetMinX(self.frame), toY, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
        self.alpha = 1.0f;
        backView.backgroundColor = [self backCustomColor];
    }];
}

/// 顶部弹框
/// - Parameter superView: 父view
- (void)topAnimateWithSuperView:(UIView *)superView {
    self.center = superView.center;
    CGFloat fromY = -CGRectGetHeight(self.frame);
    CGFloat toY = 0.0f;
    self.frame = CGRectMake(CGRectGetMinX(self.frame), fromY, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    self.alpha = 0.0;
    UIView *backView = [self backgroundViewWithFrame:superView.bounds];
    [backView addSubview:self];
    [superView addSubview:backView];
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(CGRectGetMinX(self.frame), toY, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
        self.alpha = 1.0f;
        backView.backgroundColor = [self backCustomColor];
    }];
}

#pragma mark 背景色
- (UIView *)backgroundViewWithFrame:(CGRect)frame {
    UIView *backView = [[UIView alloc] initWithFrame:frame];
    backView.tag = 9999;
    backView.backgroundColor = [UIColor clearColor];
    backView.userInteractionEnabled = self.hl_contextDismiss;
    if (self.hl_contextDismiss) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
        tap.delegate = self;
        [backView addGestureRecognizer:tap];
    }
    return backView;
}

- (UIColor *)backCustomColor {
    return [[UIColor blackColor] colorWithAlphaComponent:0.3];
}

/// - delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    UIView *backView = [self.superview viewWithTag:9999];
    if (backView) {
        if ([touch.view isEqual:backView]) {
            return YES;
        } else {
            return NO;
        }
    }
    return YES;
}

#pragma mark 消失
- (void)dismiss {
    [self dismissWithCompletion:nil];
}

- (void)dismissWithCompletion:(void(^__nullable)(void))completion {
    switch (self.hl_showType) {
        case ShowAlertType:
            [self dismissAnimateWithAlertWithCompletion:completion];
            break;
        case ShowAlertFromTopType:
            [self dismissAnimateWithAlertFromTopWithCompletion:completion];
            break;
        case ShowAlertFromBottomType:
            [self dismissAnimateWithAlertFromBottomWithCompletion:completion];
            break;
        case ShowBottomType:
            [self dismissAnimateWithBottomWithCompletion:completion];
            break;
        case ShowTopType:
            [self dismissAnimateWithTopWithCompletion:completion];
            break;
        case ShowLeftType:
            [self dismissAnimateWithLeftWithCompletion:completion];
            break;
        case ShowRightType:
            [self dismissAnimateWithRightWithCompletion:completion];
            break;
        default:
            break;
    }
}


/// alert 消失
/// - Parameter completion: 消失回调
- (void)dismissAnimateWithAlertWithCompletion:(void(^__nullable)(void))completion {
    UIView *backView = [self.superview viewWithTag:9999];
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0.0f;
        backView.backgroundColor = [UIColor clearColor];
    } completion:^(BOOL finished) {
        [backView removeFromSuperview];
        if (completion) {
            completion();
        }
    }];
}

/// alert从底部消失
/// - Parameter completion: 消失回调
- (void)dismissAnimateWithAlertFromTopWithCompletion:(void(^__nullable)(void))completion {
    UIView *backView = [self.superview viewWithTag:9999];
    CGFloat toY = -CGRectGetHeight(self.frame);
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0.0f;
        backView.backgroundColor = [UIColor clearColor];
        self.frame = CGRectMake(CGRectGetMinX(self.frame), toY, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    } completion:^(BOOL finished) {
        [backView removeFromSuperview];
        if (completion) {
            completion();
        }
    }];
}

/// alert从底部消失
/// - Parameter completion: 消失回调
- (void)dismissAnimateWithAlertFromBottomWithCompletion:(void(^__nullable)(void))completion {
    UIView *backView = [self.superview viewWithTag:9999];
    CGFloat toY = CGRectGetMaxY(self.superview.frame);
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0.0f;
        backView.backgroundColor = [UIColor clearColor];
        self.frame = CGRectMake(CGRectGetMinX(self.frame), toY, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    } completion:^(BOOL finished) {
        [backView removeFromSuperview];
        if (completion) {
            completion();
        }
    }];
}

/// 右侧消失
/// - Parameter completion: 消失回调
- (void)dismissAnimateWithRightWithCompletion:(void(^__nullable)(void))completion{
    UIView *backView = [self.superview viewWithTag:9999];
    CGFloat toX = CGRectGetMaxX(backView.frame);
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0.0f;
        backView.backgroundColor = [UIColor clearColor];
        self.frame = CGRectMake(toX, CGRectGetMinY(self.frame), CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    } completion:^(BOOL finished) {
        [backView removeFromSuperview];
        if (completion) {
            completion();
        }
    }];
}

/// 左侧消失
/// - Parameter completion: 消失回调
- (void)dismissAnimateWithLeftWithCompletion:(void(^__nullable)(void))completion{
    UIView *backView = [self.superview viewWithTag:9999];
    CGFloat toX = CGRectGetMinX(self.frame) - CGRectGetWidth(self.frame);
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0.0f;
         backView.backgroundColor = [UIColor clearColor];
        self.frame = CGRectMake(toX, CGRectGetMinY(self.frame), CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    } completion:^(BOOL finished) {
        [backView removeFromSuperview];
        if (completion) {
            completion();
        }
    }];
}

/// 底部消失
/// - Parameter completion: 消失回调
- (void)dismissAnimateWithBottomWithCompletion:(void(^__nullable)(void))completion {
    UIView *backView = [self.superview viewWithTag:9999];
    CGFloat toY = CGRectGetMaxY(self.frame) + CGRectGetHeight(self.frame);
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0.0f;
        backView.backgroundColor = [UIColor clearColor];
        self.frame = CGRectMake(CGRectGetMinX(self.frame), toY, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    } completion:^(BOOL finished) {
        [backView removeFromSuperview];
        if (completion) {
            completion();
        }
    }];
}

/// 顶部消失
/// - Parameter completion: 消失回调
- (void)dismissAnimateWithTopWithCompletion:(void(^__nullable)(void))completion {
    UIView *backView = [self.superview viewWithTag:9999];
    CGFloat toY = -CGRectGetHeight(self.frame);
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0.0f;
        backView.backgroundColor = [UIColor clearColor];
        self.frame = CGRectMake(CGRectGetMinX(self.frame), toY, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    } completion:^(BOOL finished) {
        [backView removeFromSuperview];
        if (completion) {
            completion();
        }
    }];
}

#pragma mark - Associated getter setter
- (void)setHl_showType:(MGShowType)fd_showType {
    objc_setAssociatedObject(self, hl_showTypeKey, [NSNumber numberWithUnsignedInteger:fd_showType], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (MGShowType)hl_showType {
    return [objc_getAssociatedObject(self, hl_showTypeKey) unsignedIntegerValue];
}

- (void)setHl_contextDismiss:(BOOL)hl_contextDismiss {
    objc_setAssociatedObject(self, hl_contextDismissKey, [NSNumber numberWithBool:hl_contextDismiss], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)hl_contextDismiss {
    return [objc_getAssociatedObject(self, hl_contextDismissKey) boolValue];
}





@end
